
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.department.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;

import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.util.ApplicationUtil;
import com.application.department.entity.Department;
import com.application.department.struts.form.DepartmentForm;
import com.application.department.service.DepartmentService;
import com.application.department.commons.Constants;
import com.application.role.entity.Role;
import com.application.usertable.entity.Usertable;

/**
 * 
 * Description:DepartmentAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class DepartmentAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private DepartmentService departmentService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Department department;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getDepartmentPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchDepartment" + "开始");
		DepartmentForm department = (DepartmentForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List departmentList = null;
		resultSize = departmentService.getAllCountDepartment(department);
		pageBean = PageBean.getPageBean("departmentTab", resultSize, request);
		departmentList = departmentService.searchDepartment(department,
				pageBean);
		request.setAttribute("departmentList", departmentList);

		log.debug("searchDepartment" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddDepartment(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddDepartments");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addDepartment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addDepartments");
		Department department = this.getDepartment(form);
		department.setId(RandomKey.generateID_Long_19().toString());
		department.setCreateDate(new Date());

		this.departmentService.insertObject(department);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteDepartment(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteDepartments");
		DepartmentForm departmentForm = (DepartmentForm) form;
		this.departmentService.deleteDepartments(departmentForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateDepartment(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateDepartments");
		DepartmentForm departmentForm = (DepartmentForm) form;
		department = (Department) this.departmentService.searchDepartment(
				departmentForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, department);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateDepartment(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateDepartments");
		Department department = this.getDepartment(form);
		department.setCreateDate(new Date());
		this.departmentService.updateObject(department);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewDepartment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewDepartment");
		DepartmentForm departmentForm = (DepartmentForm) form;
		department = (Department) this.departmentService.searchDepartment(
				departmentForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, department);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the DepartmentService
	 */
	public DepartmentService getDepartmentService() {
		return this.departmentService;
	}

	/**
	 * @param DepartmentService
	 *            the DepartmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * 获取Department实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Department
	 */
	public Department getDepartment(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		DepartmentForm departmentForm = (DepartmentForm) form;
		department = new Department();
		BeanUtils.copyProperties(department, departmentForm);
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public ActionForward creatTreeSelect(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/text;charset=UTF-8");
		DepartmentForm departmentForm = new DepartmentForm();
		departmentForm.setFid(request.getParameter("pid"));
		String vid = request.getParameter("treeid");
		int tjs = Integer.parseInt(request.getParameter("js"));
		Long pid = Long.parseLong(request.getParameter("pid"));
		ServletContext sc = request.getSession().getServletContext();
//		WebApplicationContext ctx = (WebApplicationContext)sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
		ApplicationUtil au = new ApplicationUtil();
		List<Department> accList = departmentService.searchDepartment(departmentForm);
		String type="bm";
		String html="";
		if(request.getParameter("type")!=null&&!request.getParameter("type").equals("")){
			type = request.getParameter("type");
		}
		if(type.equals("bm")){
			html = creatTreeSelect(accList, vid, tjs);
		}
		if(type.equals("bms")){
			html = creatTreeSelectS(accList, vid, tjs);
		}
		if(type.equals("user")){
			String rname = request.getParameter("rname");
			Role role = au.getAllRoleByName(rname);
			List<Usertable> uList = au.getUserById(role.getId(), request.getParameter("pid"));
			html = creatTreeSelectAndUser(accList,uList,vid, tjs);
		}
		if(type.equals("user_bm_o")||type.equals("user_bm_n")){
			List<Usertable> uList = au.getUserByIdBm(request.getParameter("pid"));
			html = creatTreeSelectAndUserBm(accList,uList,vid, tjs,type);
		}
		java.io.PrintWriter out = response.getWriter();
		out.print(html);
		out.flush();
		out.close();
		return null;
	}

	public String creatTreeSelect(List<Department> accList, String vid, int js) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Department department = (Department) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId() + "');\" >"
					+ "<img src='images/close1.gif' id='img" + vid + js + i
					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId()
					+ "');\" style=\"cursor:hand\">&nbsp;";

			html = html + "<a onclick=\"selectTree('" + department.getId()
					+ "','" + department.getName()
					+ "')\" style=\"cursor:hand\">" + department.getName()
					+ "</a>";
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		return html;
	}
	public String creatTreeSelectS(List<Department> accList, String vid, int js) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Department department = (Department) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + "<img src='images/img-10.gif'  id='imgt" + vid + js + i
					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId() + "');\" >";
			html = html + "&nbsp;&nbsp;<a onclick=\"selectTree('" + department.getId()
					+ "','" + department.getName()
					+ "')\" style=\"cursor:hand\">" + department.getName()
					+ "</a>";
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		return html;
	}
	public String creatTreeSelectAndUser(List<Department> accList,List<Usertable> uList, String vid, int js) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Department department = (Department) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId() + "');\" >"
					+ "<img src='images/close1.gif' id='img" + vid + js + i
					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId()
					+ "');\" style=\"cursor:hand\">&nbsp;";

			html = html + "" + department.getName();
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		for (int i = 0; i < uList.size(); i++) {
			Usertable user = (Usertable) uList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + ""
			+ "<img src='images/user.gif'>&nbsp;";
			html = html + "<a onclick=\"selectTree('" + user.getId()
			+ "','" + user.getRname()
			+ "')\" style=\"cursor:hand\">" + user.getRname()
			+ "</a>";
			html = html + "<div ><span class='f1'>.</span></div>";
		}
		return html;
	}
	public String creatTreeSelectAndUserBm(List<Department> accList,List<Usertable> uList, String vid, int js,String type) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Department department = (Department) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId() + "');\" >"
					+ "<img src='images/close1.gif' id='img" + vid + js + i
					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
					+ (js + 1) + ",'" + department.getId()
					+ "');\" style=\"cursor:hand\">&nbsp;";

			html = html + "" + department.getName();
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		for (int i = 0; i < uList.size(); i++) {
			Usertable user = (Usertable) uList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
			if(type.equals("user_bm_o")){
				html = html + ""
				+ "<img src='images/user.gif'>&nbsp;";
				html = html + "<a onclick=\"selectTree('" + user.getId()
				+ "','" + user.getRname()
				+ "')\" style=\"cursor:hand\">" + user.getRname()
				+ "</a>";
			}
			else{
				html = html + ""
				+ "<img src='images/user.gif'>&nbsp;";
				html = html + "<a onclick=\"selectTrees('" + user.getId()
				+ "','" + user.getRname()
				+ "')\" style=\"cursor:hand\">" + user.getRname()
				+ "</a>";				
			}
			
			html = html + "<div ><span class='f1'>.</span></div>";
		}
		return html;
	}
}
